# coding=utf8

import requests
import pymongo
from lxml import etree
import re
import json
import aiohttp
import fake_useragent
import datetime
from requests import Session
import time
import pandas
import redis
import itertools

location = 'fake_useragent_0.1.10.json'

ua = fake_useragent.UserAgent(path=location)

from urllib.parse import quote_plus
from pymongo import MongoClient

uri = "mongodb://%s:%s@%s" % (quote_plus('zane'), quote_plus('*#06#'), '121.9.245.183')
client = MongoClient(uri)
db = client['Homestay']
xzdz = db['xiaozhu']
xzdz_house_list = db['xiaozhuhouselist']
xzdz_pagesource = db['xiaozhupagesource']
xzdz_task = db['xiaozhutaskhrefs']

list_distric = ["番禺", "天河", "海珠", "越秀", "荔湾", "从化", "白云", "花都", "黄埔", "增城", "南沙"]
dict_district = {'从化': 'conghua', '南沙': 'nansha', '增城': 'zengcheng', '天河': 'tianhe', '海珠': 'haizhu', '番禺': 'fanyu', '白云': 'baiyun', '花都': 'huadu', '荔湾': 'liwan', '越秀': 'yuexiu', '黄埔': 'huangpu'}

booking_xpath_today = "//span[contains(text(),'今天')]/child::span"

# 找到各个行政区的入口页面url  遍历各个区的所有列表页，找到每一间民宿的href，存入数据库
from selenium import webdriver

REDIS_HOST = '192.168.10.9'
REDIS_PORT = 6379
REDIS_PASSWD = 123456
# conn = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWD)
redisPool=redis.ConnectionPool(host=REDIS_HOST, password=REDIS_PASSWD, port=REDIS_PORT, db=5)

def task_injection():
    df_goals = pandas.DataFrame(list(xzdz_house_list.find({})))
    df_vals = df_goals['hrefs'].values
    total_tasks = []
    for ll in df_vals:
        if type(ll) == list:
            total_tasks += ll
        else:
            print("fick")
    goals_sort = list(set(total_tasks))


    r = redis.Redis(connection_pool=redisPool)
    for i in goals_sort:
        r.lpush('task_xiaozhu', i.encode('utf-8'))
    # conn.set("task_xiaozhu", goals_sort)




if __name__ == "__main__":
    task_injection()
